# -*- coding: utf-8 -*-
#高级特性----生成器，在Python中，这种一边循环一边计算的机制，称为生成器：generator。

import io
import sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf-8')


list1 = (x * x for x in range(1,11))
print(next(list1))
print(next(list1))
print(next(list1))

for x in list1:
	print(x)

def fib(max):
	n,a,b = 0,0,1
	while n < max:
		print(b)
		a,b = b,a + b
		n = n + 1
	return 'done'

print(fib(10))


def odd():
    print('step 1')
    yield 1
    print('step 2')
    yield(3)
    print('step 3')
    yield(5)

o = odd()

for i in o:
	print(i)

def fib_generator(max):
	n,a,b = 0,0,1
	while n < max:
		yield b
		a,b = b,a + b
		n = n + 1
	return 'done'

for x in fib_generator(10):
	print(x)

g = fib_generator(10)

while True:
	try:
		x = next(g)
		print("g:",x)
	except StopIteration  as e:
		print('Generator return value:', e.value)
		break


#输出杨辉三角
def triangles():
	N = [1]
	while True:
		yield N
		N = [1] + [N[i]+N[i+1] for i in range(len(N) - 1)] + [1]
	
n = 0
results = []
for t in triangles():
    print(t)
    results.append(t)
    n = n + 1
    if n == 10:
        break